﻿#include "pch.h"
#include "ChapterCollection.h"
#include "ChapterCollection.g.cpp"

namespace winrt::NovelReader::implementation
{

    winrt::com_array<winrt::NovelReader::Chapter> ChapterCollection::ChaptersArray() const
    {
        return winrt::com_array<winrt::NovelReader::Chapter>(m_chaptersArray); 
    }

    void ChapterCollection::ChaptersArray(winrt::array_view<winrt::NovelReader::Chapter const> value)
    {
        m_chaptersArray.clear(); 
        std::copy(value.begin(), value.end(), std::back_inserter(m_chaptersArray)); 
    }

    void ChapterCollection::Add(winrt::NovelReader::Chapter const& chapter)
    {
        m_chaptersArray.push_back(chapter); 
    }

    void ChapterCollection::Add(winrt::hstring const& title, winrt::hstring const& text, int32_t index)
    {
        m_chaptersArray.push_back({title, text, index}); 
    }

    winrt::NovelReader::Chapter ChapterCollection::GetAt(int32_t index)
    {
        if (index >= 0 && index < m_chaptersArray.size()) {
            return m_chaptersArray[index]; 
        }
        else {
            return nullptr; 
        }
    }
}
